"use strict";
import axios from "axios";
import {PATH} from "@/utils";


// #ifdef APP-PLUS
const config = {
    baseURL: PATH, // 正式环境
    timeout: 60 * 1000, // Timeout,
    headers: {

    }
};
// #endif

//#ifdef H5
const config = {
    baseURL: "/api", // 正式环境
    timeout: 60 * 1000, // Timeout,
    headers: {

    }
};
//#endif

const _axios = axios.create(config);
_axios.interceptors.request.use(
    config => {
        // Do something before request is sent
        return config;
    },
    error => {
        // Do something with request error
        return Promise.reject(error);
    }
);
// Add a response interceptor
_axios.interceptors.response.use(
    response => {
        if(response.data.status !== 200){
            uni.showToast({
                title:response.data.msg,
                icon:'none'
            })
            return Promise.reject(response)
        }else{
            return response;
        }

    },
    error => {
        // Do something with response error
        /*switch (error.response.status) {
            case 400:
                console.log("错误请求");
                break;
            case 401:

                break;
            case 403:
                console.log("拒绝访问");
                break;
            case 404:
                console.log("请求错误，未找到该资源");
                break;
            case 405:
                console.log("请求方法未允许");
                break;
            case 408:
                console.log("请求超时");
                break;
            case 500:
                console.log("服务器端出错");
                break;
            case 501:
                console.log("网络未实现");
                break;
            case 502:
                console.log("网络错误");
                break;
            case 503:
                console.log("服务不可用");
                break;
            case 504:
                console.log("网络超时");
                break;
            case 505:
                console.log("http版本不支持该请求");
                break;
            default:
                console.log(error.message);
                break;
        }*/
        return Promise.reject(error);
    }
);
axios.defaults.adapter = function (config) { //自己定义个适配器，用来适配uniapp的语法
    return new Promise((resolve, reject) => {
        const settle = require('axios/lib/core/settle');
        const buildURL = require('axios/lib/helpers/buildURL');
        uni.request({
            method: config.method.toUpperCase(),
            url: config.baseURL + buildURL(config.url, config.params, config.paramsSerializer),
            header: config.headers,
            data: config.data,
            dataType: config.dataType,
            responseType: config.responseType,
            sslVerify: config.sslVerify,
            complete: function complete(response) {
                response = {
                    data: response.data,
                    status: response.statusCode,
                    errMsg: response.errMsg,
                    header: response.header,
                    config: config
                };
                settle(resolve, reject, response);
            }
        })
    })
}
export default _axios
